<template>
  <div class="app-container">

    <el-table v-loading="loading" :data="evalProjectList">
      <el-table-column label="学期" align="center" prop="semName" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="'/evaluate/student/' + scope.row.id" class="link-type" v-if="scope.row.status == '1'">
             <i class="el-icon-view"></i>评价
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评教项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学期" prop="semId">
            <el-select v-model="form.semId" filterable clearable placeholder="请选择学期">
              <el-option
                v-for="item in semsterList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
                size="small">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="班级" prop="className" v-if="form.id != null">
          <el-input v-model="form.className" placeholder="请输入班级" disabled/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEvalProject, getEvalProject, changeStatus, delEvalProject, addEvalProject, updateEvalProject, exportEvalProject } from "@/api/evaluate/evalProject";
import { selectSpecialty} from "@/api/base/specialty";
import { selectSemster} from "@/api/base/semster";
import { selectClazz } from "@/api/base/clazz";

export default {
  name: "MyEvalProject",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评教项目表格数据
      evalProjectList: [],
      // 学期数据
      semsterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn:"t.id",
        isAsc:"DESC",
        semId: null,
        classId: null,
        startTime: null,
        endTime: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        semId: [
          { required: true, message: "学期不能为空", trigger: "blur" }
        ],
        classId: [
          { required: true, message: "班级不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始使用时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束使用时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("common_status").then(response => {
      this.statusOptions = response.data;
    });
    selectSemster({'status':1}).then(response => {
      this.semsterList = response.rows;
    });
  },
  methods: {
    /** 查询评教项目列表 */
    getList() {
      this.loading = true;
      listEvalProject(this.queryParams).then(response => {
        this.evalProjectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
     /** 查询班级列表 */
    getFormClazzList() {
      this.loading = true;
      var p = {status:'1'};
      if(this.form.year) p.year = this.form.year;
      if(this.form.specId) p.specId = this.form.specId;
      selectClazz(p).then(response => {
        this.formClazzList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
     /** 查询班级列表 */
    getQueryClazzList() {
      this.loading = true;
      var p = {status:'1'};
      if(this.queryParams.year) p.year = this.queryParams.year;
      if(this.queryParams.specId) p.specId = this.queryParams.specId;
      selectClazz(p).then(response => {
        this.queryClazzList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        semId: null,
        classId: null,
        className: null,
        startTime: null,
        endTime: null,
        status: "1"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "生成评教项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEvalProject(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改评教项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEvalProject(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvalProject(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除评教项目编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEvalProject(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有评教项目数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportEvalProject(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
